package com.djzhou.gmms.tag.page;


import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.struts2.components.Component;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * * 分页逻辑Bean *
 * 
 * @author tangs
 */
public class Page extends Component {
	private String currentPage;
	private String totalPage;
	private String cssClass;
	private String showPageNumber;

	// 定义分页标签的显示类型
	private String type;

	// 指定首页，末页，" + nextPage + "，" + endPage + "在页面上显示时的字符串
	private String firstPage;
	private String endPage;
	private String prePage;
	private String nextPage;
	
	private String options;

	// 标识记录在数据库中的位置
	private String index;
	// 总记录条数
	private String total;
	
	//分页标签的标识，当在同一个页面上有多个标签是用于区分不同的标签
	private String id;

	private String	showPosition;
	
	
	public String getShowPosition() {
		return showPosition;
	}

	public void setShowPosition(String showPosition) {
		this.showPosition = showPosition;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(String totalPage) {
		this.totalPage = totalPage;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getShowPageNumber() {
		return showPageNumber;
	}

	public void setShowPageNumber(String showPageNumber) {
		this.showPageNumber = showPageNumber;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setFirstPage(String firstPage) {
		this.firstPage = firstPage;
	}

	public void setEndPage(String endPage) {
		this.endPage = endPage;
	}

	public void setPrePage(String prePage) {
		this.prePage = prePage;
	}

	public void setNextPage(String nextPage) {
		this.nextPage = nextPage;
	}

	public Page(ValueStack arg0) {
		super(arg0);
	}

	public void setIndex(String index) {
		this.index = index;
	}

	public void setTotal(String total) {
		this.total = total;
	}

	@Override
	public boolean start(Writer writer) {
		boolean result = super.start(writer);
		if (showPageNumber == null)
			showPageNumber = "5";
		if (cssClass == null)
			cssClass = "pagnation";
		if (type == null)
			type = "multi";
		if (firstPage == null)
			firstPage = "第一页";
		if (endPage == null)
			endPage = "最后页";
		if (prePage == null)
			prePage = "上一页";
		if (nextPage == null)
			nextPage = "下一页";
		if (index == null)
			index = "1";
		if (currentPage == null)
			currentPage = "1";
		if(totalPage == null)
			totalPage = "1";
		if(total == null) 
			total = "1"	;
		if(showPosition == null ){
			showPosition ="";
		}

		totalPage = parse(totalPage);
		currentPage = parse(currentPage);
		index = parse(index);
		total = parse(total);
		cssClass = parse(cssClass);
		showPageNumber = parse(showPageNumber);
		endPage = parse(endPage);
		firstPage = parse(firstPage);
		type = parse(type);
		prePage = parse(prePage);
		nextPage = parse(nextPage);
		if(id == null) {
			id = "";
		}
		showPosition = parse(showPosition);
		
		if(options == null){
			options = "";
		}
		
		firstPage = "第一页";
		prePage = "上一页";
		nextPage = "下一页";
		endPage = "最末页";
		
		return result;
	}
	
	private String parse(String value) {

		String actualValue = null;

		if (value == null) {
			value = "top";
		}

		if (altSyntax()) {
			if (value.startsWith("%{") && value.endsWith("}")) {
				value = value.substring(2, value.length() - 1);
			}
		}

		actualValue = (String) getStack().findValue(value, String.class);
		if(actualValue == null) {
			actualValue = value;
		}
		return actualValue;
	}

	@Override
	public boolean end(Writer writer, String body) {
		try {
			StringBuilder outputStringBuilder = new StringBuilder();
			boolean isValid = true;

			if (isValid) {
				Integer currentPageInt = Integer.valueOf(currentPage);
				Integer totalPageInt = Integer.valueOf(totalPage);
				Integer showPageNumberInt = Integer.valueOf(showPageNumber);
				String current = "";

				if(currentPageInt > totalPageInt){
					currentPageInt = totalPageInt;
					currentPage = currentPageInt+"";
				}else if(currentPageInt < 1){
					currentPageInt = 1;
					currentPage = 1+"";
				}
				
				
				if (type.equals("multi")) {

					if (totalPageInt != 0) {

						if (totalPageInt <= showPageNumberInt) { // 总的页数小于可以被显示的页码数
							if (currentPage.equals(totalPage)) { // 当前页等于总数页
								if ("1".equals(totalPage)) {
									// 如果totalPage = 1，则无需分页,不显示分页信息
								} else { // 总数页大于1
									outputStringBuilder.append("<ol class='" + cssClass + "'>");
									outputStringBuilder.append("<li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData(1)'>");
									outputStringBuilder.append(firstPage + "</a></li><li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt - 1)+")'>");
									outputStringBuilder.append(prePage + "</a></li>");
									for (int i = 1; i <= totalPageInt; i++) {
										current = "";
										if (i == currentPageInt) {
											current = " class='current' ";
										}
										outputStringBuilder.append("<li" + current + ">");
										outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+i+")'>");
										outputStringBuilder.append(i + "</a></li>");
									}
								}
							} else { // 当前页不等于总数页
								if ("1".equals(currentPage)) { // 当前页为第一页
									outputStringBuilder.append("<ol class='" + cssClass + "'>");
									for (int i = 1; i <= totalPageInt; i++) {
										current = "";
										if (i == currentPageInt) {
											current = " class='current' ";
										}
										outputStringBuilder.append("<li" + current + ">");
										outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+i+")'>");
										outputStringBuilder.append(i + "</a></li>");
									}
									outputStringBuilder.append("<li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt + 1)+")'>");
									outputStringBuilder.append(nextPage + "</a></li><li> <a title='共" + totalPageInt + "页' href='javascript:void(0)' ");
									outputStringBuilder.append("onclick='j_loadData("+totalPage+")'>");
									outputStringBuilder.append(endPage + "</a></li>");
								} else { // 当前页不为第一页
									outputStringBuilder.append("<ol class='" + cssClass + "'>");
									outputStringBuilder.append("<li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData(1)'>");
									outputStringBuilder.append(firstPage + "</a></li><li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt - 1)+")'>");
									outputStringBuilder.append(prePage + "</a></li>");
									for (int i = 1; i <= totalPageInt; i++) {
										current = "";
										if (i == currentPageInt) {
											current = " class='current' ";
										}
										outputStringBuilder.append("<li" + current + ">");
										outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+i+")'>");
										outputStringBuilder.append(i + "</a></li>");
									}
									outputStringBuilder.append("<li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt + 1)+")'>");
									outputStringBuilder.append(nextPage + "</a></li><li> <a title='共" + totalPageInt + "页' href='javascript:void(0)' ");
									outputStringBuilder.append("onclick='j_loadData("+totalPage+")'>");
									outputStringBuilder.append(endPage + "</a></li>");
								}
							}
						} else { // 总页数大于可以显示的页码数
							if (currentPage.equals(totalPage)) { // 当前页等于最后页
								outputStringBuilder.append("<ol class='" + cssClass + "'>");
								outputStringBuilder.append("<li>");
								outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData(1)'>");
								outputStringBuilder.append(firstPage + "</a></li><li>");
								outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt - 1)+")'>");
								outputStringBuilder.append(prePage + "</a></li>");
								for (int i = totalPageInt - showPageNumberInt; i <= totalPageInt; i++) {
									current = "";
									if (i == currentPageInt) {
										current = " class='current' ";
									}
									outputStringBuilder.append("<li" + current + ">");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+i+")'>");
									outputStringBuilder.append(i + "</a></li>");
								}
							} else { // 当前页不等于最后页
								if (currentPage.equals("1")) { // 当前页为第一页
									outputStringBuilder.append("<ol class='" + cssClass + "'>");
									for (int i = 1; i <= showPageNumberInt; i++) {
										current = "";
										if (i == currentPageInt) {
											current = " class='current' ";
										}
										outputStringBuilder.append("<li" + current + ">");
										outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+i+")'>");
										outputStringBuilder.append(i + "</a></li>");
									}
									outputStringBuilder.append("<li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt + 1)+")'>");
									outputStringBuilder.append(nextPage + "</a></li><li> <a title='共" + totalPageInt + "页' href='javascript:void(0)' ");
									outputStringBuilder.append("onclick='j_loadData("+totalPage+")'>");
									outputStringBuilder.append(endPage + "</a></li>");
								} else if ((currentPageInt - showPageNumberInt / 2) >= 1 && (currentPageInt + showPageNumberInt / 2) <= totalPageInt) {
									outputStringBuilder.append("<ol class='" + cssClass + "'>");
									outputStringBuilder.append("<li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData(1)'>");
									outputStringBuilder.append(firstPage + "</a></li><li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt - 1)+")'>");
									outputStringBuilder.append(prePage + "</a></li>");
									for (int i = currentPageInt - showPageNumberInt / 2; i <= (currentPageInt + showPageNumberInt / 2); i++) {
										current = "";
										if (i == currentPageInt) {
											current = " class='current' ";
										}
										outputStringBuilder.append("<li" + current + ">");
										outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+i+")'>");
										outputStringBuilder.append(i + "</a></li>");
									}
									outputStringBuilder.append("<li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt + 1)+")'>");
									outputStringBuilder.append(nextPage + "</a></li><li> <a title='共" + totalPageInt + "页' href='javascript:void(0)' ");
									outputStringBuilder.append("onclick='j_loadData("+totalPage+")'>");
									outputStringBuilder.append(endPage + "</a></li>");
								} else if ((currentPageInt - showPageNumberInt / 2) < 1) {
									outputStringBuilder.append("<ol class='" + cssClass + "'>");
									outputStringBuilder.append("<li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData(1)'>");
									outputStringBuilder.append(firstPage + "</a></li><li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt - 1)+")'>");
									outputStringBuilder.append(prePage + "</a></li>");
									for (int i = 1; i <= showPageNumberInt; i++) {
										current = "";
										if (i == currentPageInt) {
											current = " class='current' ";
										}
										outputStringBuilder.append("<li" + current + ">");
										outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+i+")'>");
										outputStringBuilder.append(i + "</a></li>");
									}
									outputStringBuilder.append("<li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt + 1)+")'>");
									outputStringBuilder.append(nextPage + "</a></li><li><a title='共" + totalPageInt + "页' href='javascript:void(0)' ");
									outputStringBuilder.append("onclick='j_loadData("+totalPage+")'>");
									outputStringBuilder.append(endPage + "</a></li>");
								} else {
									outputStringBuilder.append("<ol class='" + cssClass + "'>");
									outputStringBuilder.append("<li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData(1)'>");
									outputStringBuilder.append(firstPage + "</a></li><li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt - 1)+")'>");
									outputStringBuilder.append(prePage + "</a></li>");
									for (int i = totalPageInt - showPageNumberInt; i <= totalPageInt; i++) {
										current = "";
										if (i == currentPageInt) {
											current = " class='current' ";
										}
										outputStringBuilder.append("<li" + current + ">");
										outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+i+")'>");
										outputStringBuilder.append(i + "</a></li>");
									}
									outputStringBuilder.append("<li>");
									outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt + 1)+")'>");
									outputStringBuilder.append(nextPage + "</a></li><li> <a title='共" + totalPageInt + "页' href='javascript:void(0)' ");
									outputStringBuilder.append("onclick='j_loadData("+totalPage+")'>");
									outputStringBuilder.append(endPage + "</a></li>");
								}
							}
						}
						
						if(options.toLowerCase().indexOf("addpagejumper") != -1){
							outputStringBuilder.append("<li class='pageJumper'>第<input type='text' id='__pageJumper_index' size='1' value='"+currentPageInt+"'/><input type='hidden' value='"+currentPageInt+"' id='__pageJumper_hidden_index'/>页");
							outputStringBuilder.append("<input type='button' value='跳转' onclick='var indexStr = $(\"#__pageJumper_index\").val();var oldIndex = $(\"#__pageJumper_hidden_index\").val(); if(/^[1-9]+[0-9]*$/.test(indexStr)){   if(parseInt(indexStr)<=parseInt(\""+totalPageInt+"\")){j_loadData(indexStr);}else{$(\"#__pageJumper_index\").val(oldIndex);}}else{$(\"#__pageJumper_index\").val(oldIndex)}'/></li>");
						}
						
						outputStringBuilder.append("</ol>");
						
						
					}
				} else if (type.equals("single")) {
					outputStringBuilder.append("<ol class='" + cssClass + "'>");
					outputStringBuilder.append("<li>");
					outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData(1)'>");
					outputStringBuilder.append(firstPage + "</a></li>");
					outputStringBuilder.append("<li>");
					outputStringBuilder.append("<a href='javascript:void(0)' onclick='j_loadData("+(currentPageInt + 1)+")'>");
					outputStringBuilder.append(nextPage + "</a></li>");
					outputStringBuilder.append("</ol>");
				}
			}

			writer.write(outputStringBuilder.toString());

		} catch (IOException ex) {
			Logger.getLogger(Page.class.getName()).log(Level.SEVERE, null, ex);
		}
		return super.end(writer, body);
	}

	public void setOptions(String options) {
		this.options = options;
	}

}